<?php 

if (getParameter("edit")=="ok") echo "Mise ÃƒÂ  jour rÃƒÂ©ussie.";

if (getParameter("newstatus")!="none" && getParameter("newstatus")!=null)
{
	
	$sSql="UPDATE shop_order SET orderstatusId=".getParameter("newstatus")." WHERE id=".getParameter("id");
	
	//echo $sSql;
	$GLOBALS["conn"]->Execute($sSql);
	//echo "Location :".createRelativeUrl("edit","ok");
	
	if (getObjByKey("shop_orderstatus","code","id",getParameter("newstatus"))=="PA") //Paiement acceptÃƒÂ©
	{
		$sSql="SELECT shop_orderline.unitId, shop_orderline.articleId, shop_orderline.quantity FROM shop_orderline WHERE shop_orderline.orderId=".getParameter("id");
		$rs = ew_LoadRecordset($sSql);
		while ( !$rs->EOF)  {
			//echo $quantity." ".$articleId;
			$quantity=$rs->fields["quantity"];
			$articleId=$rs->fields["articleId"];
			updateObjByKey("shop_articles","id",$articleId,"countBuy",getObjByKey("shop_articles","countBuy","id",$articleId)+$quantity,"availability",getObjByKey("shop_articles","availability","id",$articleId)-$quantity);
			
			if ($app_shop_hasUnit=="yes"){
				$unitId=$rs->fields["unitId"];
			updateObjByKey("shop_unit","id",$articleId,"countBuy",getObjByKey("shop_unit","countBuy","id",$unitId)+$quantity,"availability",getObjByKey("shop_unit","availability","id",$unitId)-$quantity);
			}
			$rs->MoveNext();
		}
		
		
	
	
	}
	
	
	$id=getParameter("id");
	
	
	$mail=getObjByKey("portal_logins","mail","id",getObjByKey("shop_clients","loginId","id",getObjByKey("shop_order","clientId","id",getParameter("id"))));
	
	$msg="Cher ".getShopClientObjByOrderId($id,"firstname")." ".getShopClientObjByOrderId($id,"lastname")."<br/><br/>
	Le statut de votre commande NÃ‚Â° ".getShopOrderObj($id,"ref")." ÃƒÂ  ÃƒÂ©tÃƒÂ© mise ÃƒÂ  jour.<br/><br/>
	Le nouveau statut est : ".getShopOrderStatusObj(getParameter("newstatus"),"status_".$LANG).".";
	sendMailTo($mail, $msg);
	//echo $msg;
	
header("Location: ".createRelativeUrl("edit","ok"));
	
}
?>